package asia.yuanding.wechat.system.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: RedisUtil
 * @Author: YuanDing
 * @Date: 2025/7/26 15:03
 * @Description:
 */


@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 写入字符串
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 写入字符串并设置过期时间（秒）
    public void set(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    // 读取字符串
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除 key
    public Boolean del(String key) {
        return redisTemplate.delete(key);
    }

    // 判断 key 是否存在
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    // 设置过期时间
    public Boolean expire(String key, long timeout) {
        return redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    // 自增
    public Long incr(String key) {
        return redisTemplate.opsForValue().increment(key);
    }

    // 自减
    public Long decr(String key) {
        return redisTemplate.opsForValue().decrement(key);
    }
}
